import axios from 'axios'
import { apiUrl } from '@/config'
import { close, start } from '@/utils/nporgress'

const service = axios.create({
  baseURL: apiUrl,
  timeout: 5000,
})

service.interceptors.request.use(
  (config) => {
    start()
    delete config.headers.loading
    return config
  },
  (err) => {
    close()
    console.error(err)
    return Promise.reject(err)
  }
)

service.interceptors.response.use(
  ({ data: res }) => {
    close()
    if (res.status) {
      return res.data
    } else {
      console.error(res.messag);
    }
  },
  (err) => {
    close()
    console.error(err);
    return Promise.reject(err)
  }
)

export { service }
